import { stringify } from 'querystring';
import type { Effect, Reducer } from 'umi';
import { history } from 'umi';

import MenuModel from '../models/menu-slider';
import {
  getProcessCenter,
  mdopLogin,
  mdopLoginOut,
  testLogin,
} from '@/services/login';
import { setAuthority } from '@/utils/authority';
import { requestCode } from '@/utils/constans';
import { removeToken, setToken } from '@/utils/cookies';
import { getPageQuery } from '@/utils/utils';
import { Message } from 'union-design';

export type UserInfoType = {
  status?: 'ok' | 'error';
  // staffId: string | null;
  email?: string;
  loginName?: string;
  staffCode?: string;
  staffName?: string;
  token?: string;
};

export type LoginModelType = {
  namespace: string;
  state: UserInfoType;
  effects: {
    login: Effect;
    testLogin1: Effect;
    logout: Effect;
    getLoginOut: Effect;
    getProcessLogin: Effect;
  };
  reducers: {
    changeLoginStatus: Reducer<UserInfoType>;
  };
};

const Model: LoginModelType = {
  namespace: 'login',
  state: {
    status: 'ok',
  },

  effects: {
    *testLogin1({ payload }, { call, put }) {
      const response = yield call(testLogin, payload);
      const { data, status } = response;
      if (status === '00000') {
        Message.success(`${requestCode[status]}`);
        yield put({
          type: 'changeLoginStatus',
          payload: data,
        });

        setToken(data.token);
        return Promise.resolve(data);
      }
      return Promise.reject(requestCode[status]);
    },
    *login({ payload }, { call, put }) {
      const response = yield call(mdopLogin, payload);
      const { data, status, message } = response;
      if (status === '00000') {
        yield put({
          type: 'changeLoginStatus',
          payload: data,
        });

        setToken(data);
        return Promise.resolve(data);
      } else {
        Message.error(message);
      }
      return Promise.reject(requestCode[status]);
    },
    *getLoginOut(_, { call, put }) {
      const response = yield call(mdopLoginOut);
      const { data, status } = response;
      if (status === '00000') {
        return Promise.resolve(data);
      }
      return Promise.reject(requestCode[status]);
    },
    *getProcessLogin({ payload }, { call, put }) {
      const response = yield call(getProcessCenter, payload);

      const { code, msg, url } = response;
      if (code === '0000') {
        return Promise.resolve(url);
      }
      return Promise.reject(msg);
    },

    logout() {
      removeToken();
      const { redirect } = getPageQuery();
      if (window.location.pathname !== '/mdopUser/login' && !redirect) {
        history.replace({
          pathname: '/mdopUser/login',
          search: stringify({
            redirect: window.location.href,
          }),
        });
        MenuModel.state.menuNavBar = [{ path: '/system/index', name: '主页' }];
      }
    },
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      setAuthority(payload.loginName);
      return {
        ...state,
        userInfo: payload,
      };
    },
  },
};

export default Model;
